﻿using System;
using System.Collections.Generic;
using Core.Utils;

namespace Game {
    /// <summary>
    /// 武器集合体是可以挂载组件的对象
    /// 集合体，对于不同的WeaponEffect，可以作用在不同的武器的组件上而产生效果
    /// </summary>
    public abstract class ABSWeaponCombinant {
        protected Dictionary<Type, IWeaponComponent> mWeaponComponentsDic = new();
        protected Bool mTriggerEndFlag = new Bool();
        protected int mCurCounter = 0;
        protected virtual int mCounter => mWeaponComponentsDic.Count;
        
        //获得组件的接口
        public T Get<T>() where T : class, IWeaponComponent {
            if (mWeaponComponentsDic == null) {
                return null;
            }

            if (mWeaponComponentsDic.ContainsKey(typeof(T))) {
                return mWeaponComponentsDic[typeof(T)] as T;
            }

            return null;
        }
        //获得同基类的所有组件
        public T[] GetAll<T>() where T : class, IWeaponComponent {
            if (mWeaponComponentsDic == null) {
                return null;
            }
            List<T> components = new List<T>();
            foreach (var item in mWeaponComponentsDic) {
                if (typeof(T).IsAssignableFrom(item.Key)) {
                    components.Add(item.Value as T);
                }
            }
            
            return components.ToArray();
        }
        
        public bool Has<T>() where T : class, IWeaponComponent {
            foreach (var item in mWeaponComponentsDic) {
                if (typeof(T).IsAssignableFrom(item.Key)) {
                    return true;
                }
            }
            return false;
        }

        public void Add<T>(T component) where T :class, IWeaponComponent {
            if (!mWeaponComponentsDic.ContainsKey(typeof(T))) {
                mWeaponComponentsDic.Add(typeof(T),component);
            }
        }
        
        
        public void TriggerCount() {
            mTriggerEndFlag.Value = ++mCurCounter >= mCounter;
        }
    }
}